/*
 * CreateResourceDialog.java
 *
 * Created on 19 oktober 2009, 23:01
 */

package mop.gui.resource;

import javax.swing.JOptionPane;
import mop.controller.ResourceController;
import mop.exception.IllegalDescriptionException;
import mop.exception.IllegalDuplicationException;
import mop.gui.main.MainWindow;

/**
 *
 * @author  Laurens
 */
public class CreateResourceDialog extends javax.swing.JDialog {

    /** Creates new form CreateResourceDialog */
    public CreateResourceDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblCreateResource = new javax.swing.JLabel();
        lblType = new javax.swing.JLabel();
        cmbType = new javax.swing.JComboBox();
        lblDescription = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescription = new javax.swing.JTextArea();
        btnCreateResource = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Add Resource");

        lblCreateResource.setFont(new java.awt.Font("Tahoma", 1, 12));
        lblCreateResource.setText("Create Resource");
        lblCreateResource.setName("lblCreateResource"); // NOI18N

        lblType.setText("Type");
        lblType.setName("lblType"); // NOI18N

        cmbType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Room", "Tool" }));
        cmbType.setName("cmbType"); // NOI18N

        lblDescription.setText("Description");
        lblDescription.setName("lblDescription"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        txtDescription.setColumns(20);
        txtDescription.setRows(5);
        txtDescription.setName("txtDescription"); // NOI18N
        jScrollPane1.setViewportView(txtDescription);

        btnCreateResource.setText("Create Resource");
        btnCreateResource.setName("btnCreateResource"); // NOI18N
        btnCreateResource.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCreateResourceActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.setName("btnCancel"); // NOI18N
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblCreateResource)
                    .addComponent(lblType)
                    .addComponent(cmbType, 0, 380, Short.MAX_VALUE)
                    .addComponent(lblDescription)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnCreateResource, javax.swing.GroupLayout.PREFERRED_SIZE, 237, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancel, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblCreateResource)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblType)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cmbType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblDescription)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCreateResource)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

/**
 * Creates a resource.
 * 
 * @param   evt
 */
private void btnCreateResourceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCreateResourceActionPerformed
    try {
        String description = this.txtDescription.getText();

        if (this.cmbType.getSelectedItem().equals("Room")) {
            ResourceController.getInstance().createRoom(description);
            JOptionPane.showMessageDialog(null, "Room created!", "Success", JOptionPane.INFORMATION_MESSAGE);
        } else if (this.cmbType.getSelectedItem().equals("Tool")) {
            ResourceController.getInstance().createTool(description);
            JOptionPane.showMessageDialog(null, "Tool created!", "Success", JOptionPane.INFORMATION_MESSAGE);
        }//GEN-LAST:event_btnCreateResourceActionPerformed
        
        MainWindow.getInstance().update();
        this.dispose();
    } catch (IllegalDescriptionException ex) {
        JOptionPane.showMessageDialog(null, "Bad description!", "Error", JOptionPane.ERROR_MESSAGE);
    } catch (IllegalDuplicationException ex) {
        JOptionPane.showMessageDialog(null, "Resource already exists!", "Error", JOptionPane.ERROR_MESSAGE);
    }
}                                                 

/**
 * Closes the dialog.
 * 
 * @param   evt
 */
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
    this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnCreateResource;
    private javax.swing.JComboBox cmbType;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblCreateResource;
    private javax.swing.JLabel lblDescription;
    private javax.swing.JLabel lblType;
    private javax.swing.JTextArea txtDescription;
    // End of variables declaration//GEN-END:variables

}
